package com.web.action.webs;

import java.util.ArrayList;
import java.util.List;

import org.common.util.TranslateHelper;
import org.wface.web.core.ActionSupport;

import com.manager.service.message.MessageBackService;
import com.manager.service.message.MessageService;
import com.manager.service.news.CategoryService;
import com.web.model.Category;
import com.web.model.Message;
import com.web.model.vo.query.MessageQuery;
import com.web.tag.bean.PageModel;

public class MsgAction extends ActionSupport {

	MessageService msgService = new MessageService();

	private String id;
	private String title;
	private String content;
	private String page;
	
	public String init() {
		System.out.println("msg------init");
		return "";
	}
	
	public String postMsg() {

		CategoryService cateService = new CategoryService();
		Category categoryM1 = cateService.getById(21);
		getRequest().setAttribute("m1", categoryM1);
		
		
		System.out.println("msg------postMsg");
		Message msg = new Message();
		msg.setTitle(title);
		msg.setContent(content);
		msgService.save(msg);
		return list();
	}
	
	public String list() {
		System.out.println("msg------list");
		
		CategoryService cateService = new CategoryService();
		Category categoryM1 = cateService.getById(21);
		getRequest().setAttribute("m1", categoryM1);
		
		PageModel model = new PageModel();
		int pageNum = TranslateHelper.parseInteger(page);
		model.setPageNum(pageNum);
        int m=21;
		MessageQuery query=new MessageQuery();
		List<Message> messages = msgService.findList(model, query);
		
		List<Message> list=new ArrayList();//封装回复
		MessageBackService bkService=new MessageBackService();
		Message message=new Message();
		for(int i=0;i<messages.size();i++){
			message=(Message)messages.get(i);
			message.setBkList(bkService.findListByMsg(message.getMsgid()));
			list.add(message);
		}
		getRequest().setAttribute("messages", list);  
		//getRequest().setAttribute("m1", m);
		
		model.setPaths(getBasePath() + "/msg/list.do?");
		getRequest().setAttribute(PageModel.PAGEMODEL, model);
		return "list";
	}
	
	public String delete() {
		msgService.delete(TranslateHelper.parseInteger(id));
		return list();
	}

	private String getBasePath() {
		String basePath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
		return basePath;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	public void setPage(String page) {
		this.page = page;
	}
	
	public void setId(String id) {
		this.id = id;
	}
}
